package org.douxiaoban.client.util;

import java.io.IOException;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.Date;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 存储一个cookie， 实现serializable接口－做数据持久化
 * @author 6a209
 * 9:36:09 PM Apr 28, 2012
 */
public class DouCookieUtil implements Serializable{
	private static final long serialVersionUID = 1L;
//	private  CookieStore mCookie;
	
	private static DouCookieUtil sDouCookieUtil;
	private static final String KEY_LOGIN = "login_key";
	
	
	static DouCookieUtil getInstance(Context ctx){
		if(null == sDouCookieUtil){
			sDouCookieUtil = new DouCookieUtil(ctx);
		}
		return sDouCookieUtil;
	}
	
	private Context mCtx;
	
	private DouCookieUtil(Context ctx){
		mCtx = ctx;
	}
	/**
	 * 存储cookie
	 * @param cookieStore
	 */
	public void storeCookie(CookieStore cookieStore){
		String strCookie = cookie2Str(cookieStore);
		SharedPreferences preferences = 
			PreferenceManager.getDefaultSharedPreferences(mCtx);
		SharedPreferences.Editor edit = preferences.edit();
		edit.putString(KEY_LOGIN, strCookie);
		edit.commit();
	}
	
	/**
	 * 取cookie
	 * @param key
	 * @return
	 */
	public CookieStore getLoginCookieStore(){
		SharedPreferences preferences = 
			PreferenceManager.getDefaultSharedPreferences(mCtx);
		String strCookie = preferences.getString(KEY_LOGIN, "null");
		Log.v("the cookie is ", strCookie);
		if(strCookie.equals("null")){
			return null;
		}else{
			return str2Cookie(strCookie);
		}
	}
	
	
	
	/**
	 * cookie 使用的是httpclient 4.x
	 * 
	 * 当前的cookie 转换成string
	 * @return
	 * @throws IOException 
	 */
	public String cookie2Str(CookieStore cookieStore){
		Log.v("test -------------------------------------------------", cookieStore.toString());
		StringBuffer buff = new StringBuffer();
		try{
		for(int i = 0; i < cookieStore.getCookies().size(); i++){
			Cookie cookie = cookieStore.getCookies().get(i);
			String name = cookie.getName();
			String value = cookie.getValue();
			int version = cookie.getVersion();
			String path = cookie.getPath();
			String dom = cookie.getDomain();
//			cookie.
			String ex = "null";
			if(null != cookie.getExpiryDate()){
				ex = String.valueOf(cookie.getExpiryDate().getTime());
			}
			String content = name + "-:-" + value + "-:-" + version + "-:-" + path + "-:-" + dom + "-:-" + ex;
			buff.append(content);
			buff.append("-::-");
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return buff.toString();
	}	
	
	/**
	 * 将string 转成 cookie
	 * @param str 
	 * @return
	 * @throws IOException 
	 * @throws StreamCorruptedException 
	 * @throws ClassNotFoundException 
	 */
	public CookieStore str2Cookie(String str){
		BasicCookieStore cookieStore = new BasicCookieStore();
		String [] list = str.split("-::-");
		for(int i = 0; i < list.length; i++){
			String [] itemContent = list[i].split("-:-");
			BasicClientCookie cookie = new BasicClientCookie(itemContent[0], itemContent[1]);
			cookie.setVersion(Integer.parseInt(itemContent[2]));
			cookie.setPath(itemContent[3]);
			cookie.setDomain(itemContent[4]);
			if(!"null".equals(itemContent[5])){
				cookie.setExpiryDate(new Date(Long.parseLong(itemContent[5])));
			}
			cookieStore.addCookie(cookie);
		}
		return cookieStore;
		
	}
	
	
}


